//
//  TestHelper.m
//  PrefCore
//
//  Created by Sergey on 11/5/14.
//  Copyright (c) 2014 Play-on-smart. All rights reserved.
//

#import "TestHelper.h"

static PSCardSuit ParseSuit(unichar c) {
    switch (c) {
        case L'\u2660': return kSuitSpades;
        case L'\u2663': return kSuitClubs;
        case L'\u2666': return kSuitDiamonds;
        case L'\u2665': return kSuitHearts;
    }
    return kSuitNone;
}

static PSCardRank ParseRank(unichar c) {
    switch (c) {
        case L'7': return kRankSeven;
        case L'8': return kRankEight;
        case L'9': return kRankNine;
        case L'T': return kRankTen;
        case L'J': return kRankJack;
        case L'Q': return kRankQueen;
        case L'K': return kRankKing;
        case L'A': return kRankAce;
    }
    return kRankNull;
}

PSHandHash Hand(NSString *s) {
    PSHandHash res = 0;
    PSCardSuit suit = kSuitNone;
    for (int i = 0 ; i != s.length ; i++) {
        unichar c = [s characterAtIndex:i];
        PSCardSuit tmpSuit = ParseSuit(c);
        if (tmpSuit != kSuitNone) {
            suit = tmpSuit;
            continue;
        }
        PSCardRank rank = ParseRank(c);
        if (rank != kRankNull) {
            res = PSHandAddCard(res, [PSCard cardWithSuit:suit andRank:rank]);
        }
    }
    return res;
}

PSTrickHash Trick(NSString *s) {
    PSTrickHash res = 0;
    if (s.length == 0) {
        return res;
    }
    PSPlayerId pid = 0;
    for (NSString *cs in [s componentsSeparatedByString:@" "]) {
        assert(pid < 3);
        if (![cs isEqualToString:@"-"]) {
            PSCardSuit suit = ParseSuit([cs characterAtIndex:0]);
            assert(suit != kSuitNone);
            PSCardRank rank = ParseRank([cs characterAtIndex:2]);
            assert(rank != kRankNull);
            res = PSTrickAddCardPlayer(res, pid, [PSCard cardWithSuit:suit andRank:rank].compressed);
        }
        pid++;
    }
    return res;
}